package com.dq.controller.api.merchant.workbench.staff;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.StaffUserChangeQuery;
import com.dq.controller.in.AdjustingRequest;
import com.dq.controller.in.StaffQuitRequest;
import com.dq.vo.ListVo;
import com.dq.vo.Vo;

import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.record.StaffUserChangeRecord;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/10 15:12
 */
@RestController
@RequestMapping("/api/merchant/workbench/staff/change")
@Api(description = "员工变更记录")
public class StaffUserChangeRecordController extends AllService {

    @ApiOperation("员工变更记录列表")
    @PostMapping("/get")
    public ListVo get(@RequestBody StaffUserChangeQuery staffUserChangeQuery){
        List<StaffUserChangeRecord> records = staffUserChangeRecordService.findByWhere(staffUserChangeQuery);
        Long total = staffUserChangeRecordService.countByWhere(staffUserChangeQuery);
        List<StaffUserChangeRecord.StaffUserChangeRecordVo> list = new ArrayList<>();
        records.forEach(record ->{
            list.add(record.toStaffUserChangeRecordVo());
        });
        return new ListVo(list,total);
    }


    @ApiOperation("离职")
    @PostMapping("staffQuit")
    @Transactional
    public Vo staffQuit(@RequestBody StaffQuitRequest staffQuitRequest) throws AdminException {
        StaffUser staffUserQuit = dao.get(StaffUser.class,staffQuitRequest.staffQuitId);
        if(staffUserQuit == null){
            throw new AdminException("所选技师不能为空");
        }
        //转移目标技师
        StaffUser staffUser = dao.get(StaffUser.class,staffQuitRequest.getStaffUserId());
        if(staffUser == null){
            throw new AdminException("转移目标技师不能为空");
        }
        staffUserChangeRecordService.staffQuit(staffUserQuit);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("调岗")
    @PostMapping("adjusting")
    public Vo adjusting(@RequestBody AdjustingRequest adjustingRequest) throws AdminException{
        StaffUser staffUserJust = dao.get(StaffUser.class,adjustingRequest.getStaffJustId());
        //转移目标技师
        StaffUser staffUser = dao.get(StaffUser.class,adjustingRequest.getStaffUserId());
        if(staffUser == null){
            throw new AdminException("转移目标技师不能为空");
        }
        if(adjustingRequest.getOrgPositionId() == null){
            throw new AdminException("请先选择岗位");
        }
        OrgPosition orgPosition = dao.get(OrgPosition.class,adjustingRequest.getOrgPositionId());
        if(orgPosition == null){
            throw new AdminException("所选岗位不能为空");
        }
        Org org = null;
        if(adjustingRequest.getNowOrgId() != null){
            org = dao.get(Org.class, adjustingRequest.getNowOrgId());
        }
        staffUserChangeRecordService.adjusting(orgPosition,org,staffUserJust,adjustingRequest.getType());
        return new Vo(HttpStatus.OK);
    }

}
